package com.android.aiod.ui.activity;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.util.GlideUtil;
import com.android.aiod.R;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by Administrator on 2018/9/4.
 * 大图页
 */

public class BigImage_CommActitivty extends BaseActivity {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bigimage;
    }

    @Override
    protected void initView() {
        tv_page = findViewById(R.id.tv_page);
        viewpager = findViewById(R.id.viewpager);


    }

    @Override
    protected void initData() {
        imgs = (List<Object>) getIntent().getSerializableExtra("imgs");
        currentIndex = getIntent().getIntExtra("currentIndex", 0);
        if (imgs == null) {
            imgs = new ArrayList<>();
        }

        adapter = new BigImageCommPageAdapter(mContext, imgs, null);
        viewpager.setOffscreenPageLimit(imgs.size() - 2);
        viewpager.setAdapter(adapter);

        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tv_page.setText((viewpager.getCurrentItem() + 1) + "/" + imgs.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewpager.setCurrentItem(currentIndex);

        tv_page.setText((currentIndex + 1) + "/" + imgs.size());

        adapter.setPhotoViewClickListener(new PhotoViewClickListener() {
            @Override
            public void OnPhotoTapListener(View view, float v, float v1) {
                finish();
            }
        });

        if (imgs == null) {
            tv_page.setVisibility(View.GONE);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Glide.get(this).clearMemory();
        System.gc();
    }

    List<Object> imgs;
    int currentIndex = 0;

    TextView tv_page;
    ViewPager viewpager;
    BigImageCommPageAdapter adapter;

    public class BigImageCommPageAdapter extends PagerAdapter {

        private List<Object> imgs;
        private Context context;
        public PhotoViewClickListener listener;

        public BigImageCommPageAdapter(Context context, List<Object> imgs1, CommCallBack callBack) {
            this.context = context;
            this.imgs = imgs1;
        }

        public void setPhotoViewClickListener(PhotoViewClickListener listener) {
            this.listener = listener;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(context);
            Object url = imgs.get(position);
            GlideUtil.displayImage(context, url, photoView,-1);

//        photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
//            @Override
//            public void onPhotoTap(View view, float x, float y) {
//                if (listener != null) listener.OnPhotoTapListener(view, x, y);
//            }
//        });
            photoView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
                @Override
                public void onViewTap(View view, float x, float y) {
                    if (listener != null) listener.OnPhotoTapListener(view, x, y);
                }
            });
            container.addView(photoView);
            return photoView;
        }

        @Override
        public int getCount() {
            return imgs == null ? 0 : imgs.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }


    }

    public interface PhotoViewClickListener {
        void OnPhotoTapListener(View view, float v, float v1);
    }

}
